package com.songcan.common.config;

import com.songcan.common.context.JwtFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务服务自定义拦截器 配合使用 ThreadLocal 保护变量
 */
@Configuration
public class UserContextFilter {

    /**
     * 进行所有路由拦截,可进行定义匹配规则
     * @param redisTemplate
     * @return
     */
    @Bean
    public FilterRegistrationBean filterRegistrationBean(RedisTemplate redisTemplate) {
        FilterRegistrationBean<JwtFilter> registrationBean = new FilterRegistrationBean<>();
        JwtFilter httpBasicFilter = new JwtFilter(redisTemplate);
        registrationBean.setFilter(httpBasicFilter);
        List<String> urlPatterns = new ArrayList<>();
        urlPatterns.add("/*");
        registrationBean.setUrlPatterns(urlPatterns);
        return registrationBean;
    }
}
